class Solution {
public:
    bool check(string s) {
        int l = 1;
        if (s[0] == '-' || s.back() == '-') return false;
        for (int i = 0; i < s.size(); i ++ ) {
            if (s[i] >= '0' && s[i] <= '9') return false;
            if (s[i] == '-') {
                if (isalpha(s[i - 1]) && isalpha(s[i + 1]) && l != 0) l -- ; 
                else return false;
            } 
            if ((s[i] == '.' || s[i] == ',' || s[i] == '!') && (i != s.size() - 1)) return false;
        }
        return true;
    }
    
    int countValidWords(string str) {
        stringstream sin(str);
        string s;
        int ans = 0;
        while (sin >> s) {
            if (check(s))
               { ans ++;}
        }
        return ans;
    }
};
